import { defineStore } from 'pinia'

import { i18n } from '@/locales/index'

export enum Language {
  EN = 'en',
  CN = 'zh',
}

export const useLanguageStore = defineStore('language', {
  state: () => ({
    language: Language.EN,
  }),
  actions: {
    setLanguage(language: Language) {
      this.language = language
      i18n.global.locale.value = language
      localStorage.setItem('language', language)
    },
    init() {
      const curLang = localStorage.getItem('language') as Language
      if (curLang) {
        this.language = curLang
        i18n.global.locale.value = curLang
      }
    },
  },
  getters: {
    currentLanguage: (state) => state.language,
  },
})
